/* 
 * File:   Client.h
 * Author: faraj
 *
 * Created on October 6, 2009, 1:13 AM
 */

#ifndef _CLIENT_H
#define	_CLIENT_H

#include <stdio.h>
#include "../CTools/CTools.h"
#include "../testSev/COMMANDS.h"
#include "../testSev/NOTIFICATION.h"
#include "Display.h"
#include "KeyboardInput.h"
#include "Globaldata.h"

static void runClient(int port)
{
    system("clear");
    CSocket *sock;
    cout << "\nBunChi Client version 1.0" <<  endl;
    cout << "______________________________________________________________________" << endl << endl;
    doneCaptureKeys = false;
    
    pthread_t tid,rtid;
    while(true)
    {
        string ipval;
        cout << "connect to : ";
        cin >> ipval;
        
        
        sock = new CSocket(ipval,port);
      
            
        if(!sock->isConnected())
        {
            cout << "connection failed to " << ipval << " on port " << port << endl;
            continue;
        }
        
        Pthread_create(&tid,NULL,display,sock);
        Pthread_create(&rtid,NULL,getInputFromKeyBoard,NULL);
        
        while(sock->isConnected())
        {
            if(!doneCaptureKeys || val.size() == 0)
            {
                usleep(100);
                continue;
            }
            
            
            if(val.find(COMMAND_PUT_FILE)==0)
            {
               vector<string> vdata;
               Tokenize(val,vdata);
               if(vdata.size() != 2)
               {
                   cout << "usage : " << COMMAND_PUT_FILE << "<file full path>" << endl;
               }
               else
               {
                   string filePath = vdata[1];
                   int size = getFileSize(filePath);
                   char *ssize = new char[10];
                   sprintf(ssize,"%i",size);
                   vdata.clear();
                   Tokenize(filePath,vdata,"/");
                   
                   val = COMMAND_PUT_FILE;
                   val += " ";
                   val += vdata[vdata.size()-1];
                   val += " ";
                   val += ssize;
                   
                   
                   sock->writeln(val);
                   sock->sendFile(filePath);
               }
            }
            else if(val.find(COMMAND_GET_FILE)==0)
            {
               vector<string> vdata;
               Tokenize(val,vdata);
               if(vdata.size() != 2)
               {
                   cout << "usage : " << COMMAND_GET_FILE << "<hash>" << endl;
        
               }
               else
               {
                   sock->writeln(val);
               }
            }
            else
            {
               sock->writeln(val);
            }
            
            
            
            Pthread_mutex_lock(&mutex_cap);
                doneCaptureKeys = false;
            Pthread_mutex_unlock(&mutex_cap);
            
            Pthread_mutex_lock(&mutex_val);
                val.clear();
            Pthread_mutex_unlock(&mutex_val);
        }
        cout << "\nconnection closed" << endl;
        Pthread_cancel(rtid);
    }
}





#endif	/* _CLIENT_H */

